package com.teamclub.pay.sservices

import io.ebean.{Ebean, ExpressionList}
import com.teamclub.domain.pay.order.PayOrder
import com.teamclub.pay.sforms.PayOrderForm
import com.teamclub.util.errors.ErrorCode
import com.teamclub.util.libs.F
import com.teamclub.util.page.Pager
import org.apache.commons.lang3.StringUtils
import org.springframework.stereotype.Service

@Service
class OrderService {
  def expr(expr: ExpressionList[PayOrder], form: PayOrderForm) = {
    if(StringUtils.isNotBlank(form.organNo)) {
      expr.eq("organNo", form.organNo)
    }
    if(StringUtils.isNotBlank(form.userNo)) {
      expr.eq("userNo", form.userNo)
    }
    if(StringUtils.isNotBlank(form.orderNum)){
      expr.like("orderNum", form.orderNum)
    }
    if(StringUtils.isNotBlank(form.thirdOrderNum)) {
      expr.like("thirdOrderNum", form.thirdOrderNum)
    }
    expr.ne("payResult", "0")
  }

  def list(form: PayOrderForm): F.Either[Pager[PayOrder], ErrorCode] = {
    val where = Ebean.find(classOf[PayOrder]).where()
    expr(where, form)
    F.Either.Left(Pager.genePager(where.query(), form))
  }
}
